package com.statistics.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.statistics.bean.Electricity;
import com.statistics.bean.MaxCurrentPower;
import com.statistics.bean.Water;
import com.statistics.cloudpower.dao.TblWaterStatisticsDayDao;
import com.statistics.cloudpower.dao.TblWaterStatisticsMonthDao;
import com.statistics.cloudpower.entity.*;
import com.statistics.tools.Constants;
import com.statistics.tools.Tools;
import com.statistics.yfdata.dao.DataWaterRepository;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author MeowParse
 * @date 2017/9/1.
 */
@Component
@Scope("prototype")
public class WaterStatisticsThread extends Thread{

	private Logger logger;
	private String uuid;
	private String command;
	private List<TblEquipment> obj;
	private int fragment_number;
	private TblWaterStatisticsDayDao tblWaterStatisticsDayDao;
	private TblWaterStatisticsMonthDao tblWaterStatisticsMonthDao;
	private DataWaterRepository dataWaterRepository;
	private String month;

	public WaterStatisticsThread(String command, List<TblEquipment> obj,
	                                   Object dao, String uuid, int fragment_number,
	                                   DataWaterRepository dataWaterRepository,
	                                   String month) {

		this.command = command;
		this.obj = obj;
		this.fragment_number = fragment_number;
		this.uuid = uuid;
		this.dataWaterRepository = dataWaterRepository;
		this.month = month;

		switch (command) {
			case Constants.EVERY_DAY:
				this.logger = org.apache.log4j.Logger.getLogger("水务日统计");
				this.tblWaterStatisticsDayDao = (TblWaterStatisticsDayDao) dao;
				break;
			case Constants.EVERY_MONTH:
				this.logger = org.apache.log4j.Logger.getLogger("水务月统计");
				this.tblWaterStatisticsMonthDao = (TblWaterStatisticsMonthDao) dao;
				break;
		}
	}


	@Override
	public void run(){
		//根据读取的command进行相应运算
		try {
			DateFormat dataFormat = DateFormat.getDateInstance();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String thread_start_time = simpleDateFormat2.format(new Date());

			String now = dataFormat.format(new Date());
			Date today = simpleDateFormat.parse(now);
			Long start_date = (long) 0;//前一天起始时间的时间戳
			Long end_date = (long) 0;//前一天结束时间的时间戳

			List<TblEquipment> tblEquipment = obj;
			List records = null;
			int equipment_number;


			switch (command) {
				case Constants.EVERY_DAY:

					//每日电量统计
					start_date = today.getTime() - 24 * 3600 * 1000;
					end_date = today.getTime();
					logger.info(start_date);
					logger.info(end_date);
					records = new ArrayList<TblElectricityStatisticsDay>();
					break;
				case Constants.EVERY_MONTH:

					//每月电量统计
					Calendar first_day = Calendar.getInstance();
					first_day.add(Calendar.MONTH, -1);
					first_day.set(Calendar.DAY_OF_MONTH, 1);

					Calendar last_day = Calendar.getInstance();
					last_day.set(Calendar.DAY_OF_MONTH, 1);
					start_date = simpleDateFormat.parse(dataFormat.format(first_day.getTime())).getTime();
					end_date = simpleDateFormat.parse(dataFormat.format(last_day.getTime())).getTime();

					records = new ArrayList<TblElectricityStatisticsMonth>();

					break;
			}

			if (records == null) {
				return;
			}

			//对这n条设备数据逐条进行查找相应的电量列表
			for (equipment_number = 0; equipment_number < tblEquipment.size(); equipment_number++) {
				JSONArray jsonArray = new JSONArray();

				if (tblEquipment.get(equipment_number) == null) {
					continue;
				}

				String mac = tblEquipment.get(equipment_number).getMac();

				List<Water> waters = dataWaterRepository.getTotalWater(mac, start_date, end_date, month);

				if (waters == null) {
					//没有这条设备记录
					continue;
				}

				//获取各个房间当天用水量
				for (Water water : waters) {

					switch (command) {
						case Constants.EVERY_DAY: {
							TblWaterStatisticsDay one_record = new TblWaterStatisticsDay();
							one_record.setMac(mac);
							one_record.setOffset(water.getOffset());
							one_record.setTotalWater(water.getTotalWater());
							one_record.setStatisticsTime(new Timestamp(start_date));

							records.add(one_record);
							break;
						}
						case Constants.EVERY_MONTH: {
							TblWaterStatisticsMonth one_record = new TblWaterStatisticsMonth();
							one_record.setMac(mac);
							one_record.setOffset(water.getOffset());
							one_record.setTotalWater(water.getTotalWater());
							one_record.setStatisticsTime(new Timestamp(start_date));
							records.add(one_record);
							break;
						}
					}
					JSONObject jsonObject = Tools.getJsonWaterStr(mac,water.getOffset(),water.getTotalWater(),start_date);
					jsonArray.add(jsonObject);

				}
				switch (command) {
					case Constants.EVERY_DAY:
						Tools.sendTo61(Constants.DIANBIAODAY,jsonArray.toJSONString());
						break;
					case Constants.EVERY_MONTH:
//                        Tools.sendTo61(Constants.DIANBIAOMONTH,jsonArray.toJSONString());
						break;
				}
			}

			switch (command) {
				case Constants.EVERY_DAY:
					tblWaterStatisticsDayDao.save(records);
					break;
				case Constants.EVERY_MONTH:
					tblWaterStatisticsMonthDao.save(records);
					break;
			}

			records.clear();

			String thread_end_time = simpleDateFormat2.format(new Date());
			logger.info(uuid + " 第" + fragment_number + "分片 Count=" + equipment_number + " Start=" + thread_start_time + " End=" + thread_end_time);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
}
